//
//  XGNormalNewsCell.swift
//  TodayNews
//
//  Created by monkey on 2020/3/25.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit

protocol XGNewsProtocol
{
    var newsViewModel: XGNewsViewModel? { get set }
}

class XGNormalNewsCell: UITableViewCell, XGRegisterReusableViewProtocol, XGNewsProtocol
{
    var newsViewModel: XGNewsViewModel? {
        didSet {
            titleLabel.text = newsViewModel?.title
            infoLabel.text = newsViewModel?.info
            
            var stackViewHeight: CGFloat = 0.0
            var stackViewTop: CGFloat = 0.0
            // 有三张图片
            if let imageList = newsViewModel?.imageList {
                stackViewHeight = 80.0
                stackViewTop = 10.0
                for (index,imageInfo) in imageList.enumerated() {
                    let view = imageStackView.arrangedSubviews[index]
                    if imageInfo.url != nil {
                        (view as? UIImageView)?.kf.setImage(with: URL(string: imageInfo.url!))
                    }
                }
            }
            
            imageStackView.snp.updateConstraints { (make) in
                make.top.equalTo(titleLabel.snp.bottom).offset(stackViewTop)
                make.height.equalTo(stackViewHeight)
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 标题
    private lazy var titleLabel = UILabel(text: "测试", textColor: UIColor.black, fontSize: 17, textAlignment: .left)
    /// 描述信息
    private lazy var infoLabel = UILabel(text: "测试", textColor: UIColor.lightGray, fontSize: 13, textAlignment: .left)
    /// 三张图片
    private lazy var imageStackView: UIStackView = {
        let imageViews = [1,2,3].map { _ -> UIImageView in
            let imageView = UIImageView()
            imageView.contentMode = .center
            imageView.clipsToBounds = true
            return imageView
        }
        let stackView = UIStackView(arrangedSubviews: imageViews)
        stackView.distribution = .fillEqually
        stackView.spacing = 5.0
        return stackView
    }()
}

// MARK: - 私有扩展

private extension XGNormalNewsCell
{
    func setUpUI() -> Void
    {
        titleLabel.theme_textColor = "colors.cell_title"
        
        contentView.addSubview(titleLabel)
        contentView.addSubview(imageStackView)
        contentView.addSubview(infoLabel)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.lessThanOrEqualTo(44)
        }
    
        imageStackView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(80)
        }
        
        infoLabel.snp.makeConstraints { (make) in
            make.top.equalTo(imageStackView.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.lessThanOrEqualToSuperview().offset(-10)
        }
    }
}
